package banwood.com.xiongfasy.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.Timer;

import banwood.com.xiongfasy.BASE.BaseActivity;
import banwood.com.xiongfasy.BASE.GetMessage;
import banwood.com.xiongfasy.BASE.Urls;
import banwood.com.xiongfasy.R;
import banwood.com.xiongfasy.dialog.LoadingDialog;
import banwood.com.xiongfasy.http.GetAsynctask;
import banwood.com.xiongfasy.listener.ResultListener;
import banwood.com.xiongfasy.utils.LogUtils;
import butterknife.BindView;
import cn.shellinfo.serv.comm.ParamMap;

/*
 * 扫描付款页面
 * 
 * */
public class ScanpaymentActivity extends BaseActivity {

    private static String TAG="ScanpaymentActivity";
    @BindView(R.id.scam_pagment_money)
    TextView scamPagmentMoney;
    private ImageView scan_image;
    private Bitmap mBitmap;
    private String money;
    private ParamMap paramMap;
    public String orderNo;//订单号
    private Timer timer=new Timer();

    private int count=0;//总共请求了多少次
    private LoadingDialog dialog;


    @Override
    protected int getLayoutId() {
        return R.layout.scan_payment;
    }

    @Override
    protected void initView() {
        settitle("扫描付款");
        scan_image = obtainView(R.id.scan_image);
        money=getIntent().getExtras().getString("money");
        scamPagmentMoney.setText(money+"元");
        SpannableStringBuilder sb=new SpannableStringBuilder(scamPagmentMoney.getText().toString());
        sb.setSpan(new ForegroundColorSpan(Color.RED),0,scamPagmentMoney.getText().toString().length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.biaoti)),scamPagmentMoney.getText().toString().length()-1,scamPagmentMoney.getText().toString().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new AbsoluteSizeSpan(36,true),0,scamPagmentMoney.getText().toString().length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new AbsoluteSizeSpan(24,true),scamPagmentMoney.getText().toString().length()-1,scamPagmentMoney.getText().toString().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        scamPagmentMoney.setText(sb);
    }

    @Override
    protected void initData() {
        dialog=new LoadingDialog(ScanpaymentActivity.this,1);
        dialog.show();
        dialog.setMessage("正在生成二维码,请稍后...");
        orderNo="ON"+GetMessage.getSystemTime();
        LogUtils.d(TAG,orderNo);
        getParam();

        new GetAsynctask(new ResultListener() {
            @Override
            public void getResult(ParamMap result) {
                dialog.dismiss();
                if(result!=null &&"0000".equals(result.get("ReturnCode"))){
                    LogUtils.d(TAG,result.toString());
                    Code(result.getString("token"));
                }

            }
        },ScanpaymentActivity.this,paramMap,"merchantPayment").execute(paramMap);


    }

//    TimerTask task=new TimerTask() {
//        @Override
//        public void run() {
//            count++;
//            getMessage();
//        }
//    };

    private void getParam() {
        paramMap=new ParamMap();
        paramMap.put("PayTypeID","ImmediatePay");
        paramMap.put("OrderDate", GetMessage.getData());
        paramMap.put("OrderTime",GetMessage.getTime());
        paramMap.put("OrderNo",orderNo);
        paramMap.put("CurrencyCode","156");
        paramMap.put("orderTimeountDate","20171015104901");
        paramMap.put("OrderAmount",money);
        paramMap.put("InstallmentMark","0");
        paramMap.put("CommodityType","0201");
        paramMap.put("PaymentType","A");
        paramMap.put("PaymentLinkType","1");
        paramMap.put("NotifyType","1");
        paramMap.put("ResultNotifyURL", Urls.huidiao);
        paramMap.put("IsBreakAccount", "0");
        paramMap.put("en",MyApplication.getEnHao());
        paramMap.put("ProductName","山东雄发商贸有限公司");
    }

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    getMessage();
                    break;
            }

        }
    };

    //生成二维码
    private void Code(String code) {
        //14955939092826724015返回的token
        LogUtils.d(TAG,code);
        String textContent = "token=" + code + ",1111";
        mBitmap = CodeUtils.createImage(textContent, 360, 360, BitmapFactory.decodeResource(getResources(), R.drawable.nongxing128));
        scan_image.setImageBitmap(mBitmap);
//        timer.schedule(task,0,30*1000);
        getMessage();
    }

    //之后一直请求服务器
    public void getMessage(){
        ParamMap paramMap=new ParamMap();
        paramMap.put("orderNo",orderNo);

        new GetAsynctask(new ResultListener() {
            @Override
            public void getResult(ParamMap result) {
                if(result!=null){
                     LogUtils.d(TAG,result.toString());
//                    timer.cancel();
                    Intent intent=new Intent(ScanpaymentActivity.this,PaySuccessActivity.class);
                    intent.putExtra("param",result);
                    startActivity(intent);
                    finish();
                }else{
                    LogUtils.d(TAG,",没有请求到数据");
                   mHandler.sendEmptyMessageDelayed(1,10*1000);
                }
            }
        },ScanpaymentActivity.this,paramMap,"getMerchantPaymentReturn").execute(paramMap);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
//        timer.cancel();
        mHandler.removeMessages(1);
    }
}
